package phoenix.mob_android.page_objects;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseMobAndroidExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.mob_android.locators.base.IndexPageLocatorsBase;

/**
 * Class provides the methods for Index page
 */

public class IndexPage extends PageBaseMobAndroidExt
{
	/**
	 * Initializes the IndexPage
	 * @param p_manager
	 *            PhoenixManager instance
	 */
	public IndexPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return IndexPageLocatorsBase instance
	 */
	public IndexPageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.mob_android.locators.flirt.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.mob_android.locators.wildbuddies.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.mob_android.locators.cheeckylovers.IndexPageLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public IndexPage isOnIndexPage()
	{
		md().waitForVisibility(elements().btnLogin, "Button login doesn't appear");

		return this;
	}

	public void login(String p_email, String p_password)
	{
		md().waitForPresence(elements().btnLogin, "Button login doesn't appear");
		md().click(elements().btnLogin);
		md().waitForPresence(elements().fieldInputEmail, "Field input email doesn't appear");
		md().typeAndClear(elements().fieldInputEmail, p_email);
		md().hideKeyboardApp();
		md().typeAndClear(elements().fieldInputPassLogin, p_password);
		md().hideKeyboardApp();
		md().click(elements().btnSubmitLogin);
	}

	public IndexPage checkFirstStep()
	{
		if (md().isElementDisplayed(elements().btnOpenRegForm))
		{
			md().click(elements().btnOpenRegForm);
		}
		md().wait(2);
		md().waitForVisibility(elements().blockChooseGender, "Block choose gender doesn't appear");
		md().waitForVisibility(elements().btnNextStep, "Button next step doesn't appear");
		md().click(elements().btnNextStep);

		return this;
	}

	public IndexPage checkThirdStep()
	{
		md().waitForVisibility(elements().btnSelectRegistrationBirthday, "Button birthday doesn't appear");
		md().waitForVisibility(elements().btnSelectRegistrationBirthMonth, "Button month doesn't appear");
		md().waitForVisibility(elements().btnSelectRegistrationBirthYear, "Button year doesn't appear");
		md().click(elements().btnNextStep);

		return this;
	}

	public IndexPage checkFourthStep(TestUserDataModel p_userData, String p_regEmail, String p_correct_pass, String p_email_with_space,
			String p_email_with_point,
			String p_email_with_dog, String p_email_with_two_dog, String p_email_with_dog_and_point, String p_short_pass, String p_long_pass)
	{
		md().waitForVisibility(elements().fieldInputEmail, "Input field email doesn't appear");
		md().waitForVisibility(elements().fieldInputPass, "Input field pass doesn't appear");

		md().typeAndClear(elements().fieldInputPass, p_correct_pass);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_space);
		md().hideKeyboardApp();
		md().click(elements().btnNextStep);
		md().waitForVisibility(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " +
				p_email_with_space);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_point);
		md().hideKeyboardApp();
		md().click(elements().btnNextStep);
		md().waitForVisibility(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " +
				p_email_with_point);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_dog);
		md().hideKeyboardApp();
		md().click(elements().btnNextStep);
		md().waitForVisibility(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " +
				p_email_with_dog);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_two_dog);
		md().hideKeyboardApp();
		md().click(elements().btnNextStep);
		md().waitForVisibility(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " +
				p_email_with_two_dog);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_dog_and_point);
		md().hideKeyboardApp();
		md().click(elements().btnNextStep);
		md().waitForVisibility(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " +
				p_email_with_dog_and_point);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_userData.getEmail());
		md().typeAndClear(elements().fieldInputPass, p_short_pass);
		md().hideKeyboardApp();
		md().click(elements().btnNextStep);
		md().waitForVisibility(elements().unSuccessMess,
				"Unsuccess message of short password doesn't appear with password => " + p_short_pass);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputPass, p_long_pass);
		md().hideKeyboardApp();
		md().click(elements().btnNextStep);
		md().waitForVisibility(elements().unSuccessMess, "Unsuccess message of long password doesn't appear with password => " + p_long_pass);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputPass, p_userData.getPassword());
		md().hideKeyboardApp();
		md().click(elements().btnNextStep);

		return this;
	}

	public IndexPage checkFifthStep()
	{

		md().waitForVisibility(elements().fieldInputLocation, "Input field location doesn't appear");
		md().click(elements().btnNextStep);

		return this;
	}

	public IndexPage checkSixthStep()
	{

		md().waitForVisibility(elements().fieldInputPhone, "Input field phone doesn't appear");
		md().waitForVisibility(elements().fieldInputScreenname, "Input field screennmae doesn't appear");
		md().click(elements().btnNextStep);

		return this;
	}

	public IndexPage checkSecondStep()
	{
		md().waitForVisibility(elements().blockChooseGender, "Block choose gender doesn't appear");
		md().waitForVisibility(elements().btnNextStep, "Button next step doesn't appear");
		md().click(elements().btnNextStep);

		return this;
	}

	public void checkViewsPresence()
	{

		if (md().isElementDisplayed(elements().btnOpenRegForm))
		{
			md().click(elements().btnOpenRegForm);
		}
		md().waitForPresence(elements().btnSelectGender, "Button select gender doesn't appear");
		md().waitForPresence(elements().btnSelectRegistrationBirthday, "Button DOB doesn't appear");
		md().waitForPresence(elements().fieldInputEmail, "Field input email doesn't appear");
		md().waitForPresence(elements().fieldInputPhone, "Field input phone doesn't appear");
		md().waitForPresence(elements().fieldInputPass, "Field input password doesn't appear");
	}

	public IndexPage clickBackAndroidButton()
	{
		manager.md().driver().navigate().back();

		return this;
	}

	public void checkInvalidEmail(String p_correct_pass, String p_email_with_space, String p_email_with_point, String p_email_with_dog,
			String p_email_with_two_dog, String p_email_with_dog_and_point)
	{
		md().click(elements().btnSelectRegistrationBirthday);
		md().waitForPresence(elements().btnSubmitDOB, "Submit DOB button doesn't appear");
		md().click(elements().btnSubmitDOB);
		md().waitForPresence(elements().fieldInputPass, "Field input password doesn't appear");
		md().typeAndClear(elements().fieldInputPass, p_correct_pass);
		this.clickBackAndroidButton();
		md().typeAndClear(elements().fieldInputEmail, p_email_with_space);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().waitForPresence(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " + p_email_with_space);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_point);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().waitForPresence(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " + p_email_with_point);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_dog);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().waitForPresence(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " + p_email_with_dog);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_two_dog);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().waitForPresence(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " + p_email_with_two_dog);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputEmail, p_email_with_dog_and_point);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().waitForPresence(elements().unSuccessMess, "Unsuccess message doesn't appear with email => " + p_email_with_dog_and_point);
		md().click(elements().btnOkErrorWindow);

	}

	public void checkBirthday(String p_regEmail)
	{
		md().waitForPresence(elements().fieldInputEmail, "Input field email doesn't appear");
		md().typeAndClear(elements().fieldInputEmail, p_regEmail);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().waitForPresence(elements().unSuccessMess, "Unsuccess message DOB doesn't appear");
		md().click(elements().btnOkErrorWindow);
	}

	public void checkInvalidPass(String p_regEmail, String p_short_pass, String p_long_pass)
	{
		md().waitForPresence(elements().fieldInputEmail, "Field input email doesn't appear");
		md().typeAndClear(elements().fieldInputEmail, p_regEmail);
		this.clickBackAndroidButton();
		md().typeAndClear(elements().fieldInputPass, p_short_pass);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().waitForPresence(elements().unSuccessMess, "Unsuccess message of short password doesn't appear with password => " + p_short_pass);
		md().click(elements().btnOkErrorWindow);
		md().typeAndClear(elements().fieldInputPass, p_long_pass);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().waitForPresence(elements().unSuccessMess, "Unsuccess message of long password doesn't appear with password => " + p_long_pass);
		md().click(elements().btnOkErrorWindow);
	}

	public void registrationTest(String p_email, String p_password)
	{
		if (md().isElementPresent(elements().btnOpenRegForm))
		{
			md().click(elements().btnOpenRegForm);
		}
		md().waitForVisibility(elements().btnSelectRegistrationBirthday, "Button registration birthday doesn't appear");
		md().click(elements().btnSelectRegistrationBirthday);
		md().waitForPresence(elements().btnSubmitDOB, "Submit DOB button doesn't appear");
		md().click(elements().btnSubmitDOB);
		md().waitForPresence(elements().fieldInputEmail, "Field input email doesn't appear");
		md().typeAndClear(elements().fieldInputEmail, p_email);
		this.clickBackAndroidButton();
		md().waitForPresence(elements().fieldInputPass, "Field input password doesn't appear");
		md().typeAndClear(elements().fieldInputPass, p_password);
		this.clickBackAndroidButton();
		md().click(elements().btnRegister);
		md().wait(5);
	}

}